package cn.fjy.android.phone.worker;

import cn.fjy.android.res.Constant;
import cn.fjy.android.utils.GRPropertiesUtil;
import android.os.Vibrator;

/**
 * @author GrayF
 * @2011-3-31
 */
public class TalkTimeRemind implements Runnable {

	private String tag = "TalkTimeRemind";

	// 提示方式为震动
	public final static int REMIND_TYPE_VIBRATOR = 0;

	// 提示方式为提示音
	public final static int REMIND_TYPE_AUDIO = 1;

	// 提示方式
	public static int remind_type = REMIND_TYPE_AUDIO;

	// public static int remindDev = AudioManager.STREAM_VOICE_CALL;

	// 是否启用该功能
	public static boolean isEnable = false;

	public static int tt_vibrator_time = 100;

	private static boolean isInit = false;

	// 震动器
	private Vibrator vibrator;

	private VibratorTTRemind vibratorTTRemind;

	private AudioTTRemind audioTTRemind;
	
	private boolean active = false;

	public TalkTimeRemind(Vibrator vibrator) {
		this.vibrator = vibrator;
		initConfig();
		isInit = true;
	}

	public void initConfig() {
		GRPropertiesUtil prop = new GRPropertiesUtil(Constant.CONFIG_FILE_NAME);
		// 通话时间提示开关
		String tempValue = prop.getValue(Constant.PROP_KEY_TALKTIME_REMIND);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			if ("0".equals(tempValue)) {
				isEnable = true;
			} else {
				isEnable = false;
			}
		}

		// 通话时间提示方式
		tempValue = prop.getValue(Constant.PROP_KEY_TALKTIME_REMIND_TYPE);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			if ((TalkTimeRemind.REMIND_TYPE_VIBRATOR + "").equals(tempValue)) {
				remind_type = REMIND_TYPE_VIBRATOR;
			} else {
				remind_type = REMIND_TYPE_AUDIO;
			}
		}

		// 初始化提示震动时间
		tempValue = prop.getValue(Constant.PROP_KEY_TT_VIBRATOR_TIME);
		if (tempValue != null && !"".equals(tempValue.trim())) {
			try {
				tt_vibrator_time = Integer.valueOf(tempValue);
			} catch (Exception e) {
				tt_vibrator_time = 100;
			}
		}
	}

	@Override
	public void run() {
		// 长时间进程不活动，静态数据会被清理
		if (!isInit) {
			initConfig();
			isInit = true;
		}
		if (isEnable) {
			active = true;
			Thread remindThread = null;
			if (remind_type == REMIND_TYPE_VIBRATOR) {
				vibratorTTRemind = new VibratorTTRemind(vibrator);
				remindThread = new Thread(vibratorTTRemind);
				remindThread.start();
			} else {
				audioTTRemind = new AudioTTRemind();
				remindThread = new Thread(audioTTRemind);
				remindThread.start();
			}
		}

	}

	public void stopRemind() {
		active = false;
		if (vibratorTTRemind != null) {
			vibratorTTRemind.setEnable(false);
		}
		if (audioTTRemind != null) {
			audioTTRemind.setEnable(false);
		}
	}

	public boolean isActive() {
		return active;
	}
	
}